var tool = require("./tool");

module.exports = function (next) {
    var deployInfo = require("../../deploy-conf.json"),
        buildInfo = require("../build.json/index.json"),
        argvArr = require("../config.js"),
        params = argvArr[tool.findIn('f', argvArr, 'cmd')]["params"],
        devs = deployInfo.deploy,
        previews = buildInfo.previews,
        servers = buildInfo.servers,
        i = 0,
        tmp = [],
        item;

    if (params.length) {
        for (i = params.length; i >= 0; i--) {
            item = params[i];
            find(devs, item, "dev");
            find(previews, item, "preview");
            find(servers, item, "server");
        }

        console.log("\n");
        tmp.forEach(function (item) {
            console.log("--------------------" + item.find + "-------------------");
            console.log("alias: " + item.alias);
            console.log("receiver: " + item.receiver);
            console.log("dist: " + item.dist);
            console.log("\n");
        });
    }

    if (next && next.length) {
        next.shift()(next);
    }

    function find(maps, item, key) {
        var reg = /((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))/,
            ip = "";

        for (var i = 0, len = maps.length; i < len; i++) {
            ip = maps[i]["receiver"].match(reg);
            if (('' + item).indexOf(ip[0]) != -1) {
                tmp.push({
                    type: key,
                    alias: key + i,
                    receiver: maps[i]["receiver"],
                    dist: maps[i]["dist"],
                    find: item
                });
                return true;
            }
        }

        return false;
    }
}
;
